<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJAXMLImport extends JAJobBoardController {
	
	/**
	 * $_id;
	 * 
	 * */
	var $_id;
	/**
	 * string $_tab selected tab
	 * 
	 * */
	var $_tab;
	/**
	 * string task $_task selected task
	 * 
	 * */
	var $_task;
	/**
     * Constructor
     */
    function __construct( $location = array() )
    {
        parent::__construct( $location );  
        $this->_id = JRequest::getVar('cid',  0, '', 'array');
		checkPermission_Administrator('import_job');		
		// Get tab value
		$this->_tab = JRequest::getVar("tab", "feeds"); 
		$this->_task = JRequest::getVar("task", "list"); 
		
		// Register task
		$this->registerTask("apply", "save");
		$this->registerTask( 'orderup',    'orderdown' );
		$this->registerTask( 'unpublish',    'publish' );
		$this->registerTask( 'non-approved', 	'approved');
    }
    
    /**
    * Display current japerminission of the component to administrator
    * 
    */
    function display(){
    	
    	$this->_task = JRequest::getVar("task", "list");
    	
    	switch($this->_tab)
		{			
			case "feeds":{
				switch ($this->_task){
					case "list":						
						JRequest::setVar("layout", "items_list");						
						break;
					case "edit":
					case "add":
						JRequest::setVar("layout", "items_form");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "setupstruct":
						JRequest::setVar("layout", "items_struct");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "setupfield":
						JRequest::setVar("layout", "items_field");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "importdata":
						JRequest::setVar("layout", "items_import");
						JRequest::setVar("hidemainmenu", 1);
						break;
					case "importstatus":
						JRequest::setVar("layout", "items_status");
						JRequest::setVar("hidemainmenu", 1);
						break;
				}
			}
			break;
			
			case "logs":{
				JRequest::setVar("layout", "items_log");
				JRequest::setVar("hidemainmenu", 1);
				break;
			}
			
			case "imported":{
				switch ($this->_task){
					case "list":
						JRequest::setVar("layout", "imported_list");
						break;
				}
			}
			break;
		}
		
		parent::display();
    }
    
    function edit(){
    	$cid = JRequest::getVar("cid");
    	if($this->_tab == 'imported'){
			$this->setRedirect("index.php?option=com_jajobboard&view=jajobs&task=edit&cid[]=".$cid[0]);
			exit();
    	}
    	// Edit for feed item
    	JRequest::setVar("layout", "items_form");
		JRequest::setVar("hidemainmenu", 1);
    	parent::display();
    }
   	
    /**
	* Approved job
	*/
	/**
	 * Approved list item 
	 * @return void
	 **/
	function approved(){
		global  $option;
		
		if($this->getTask()=='approved')
		{ 
			$status = 'Approved';
			$succ_msg = JText::_('Successfully approved job item(s)!');
		}
		else 
		{
			$status = 'Non-Approved';
			$succ_msg = JText::_('Successfully disapproved job item(s)!');
		}
		
		$model = $this->getModel('jajobs');
				
		if (!$model->approved($status)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			exit();
		}
		//$cache = & JFactory::getCache('jajobs');
		//$cache->clean();	
		
		$this->setRedirect("index.php?option=$option&view=jaxmlimport&tab=imported", $succ_msg);
	}	
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
    	$tab = JRequest::getVar("tab", "categories");    	
        $this->setRedirect("index.php?option=com_jajobboard&view=jaxmlimport&tab=$tab", JText::_("Cancel Operation"));
    }   
    
    /**
    * Save XML structure
    * 
    */
    function savestruct(){
    	global $option;
    	switch ($this->_tab){    		
    		case "feeds":    			
    			$model =& $this->getModel("jaxmlimport");
    			$element = JRequest::getVar("element");
    			$FeedID = JRequest::getVar("FeedID");
    			
    			if($element != ""){
					if ($model->storeStruct($element, $FeedID)) {
						$msg = JText::_( 'Structure saved!' );
					} else {
						$msg = JText::_( 'Error saving structure' );
					}
    			}else{
    				$msg = JText::_( 'Please select the sequence element to save!' );
    			}
    							
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    } 
    
    /**
    * Save map field
    * 
    */
    function savefield(){
    	global $option;
    	switch ($this->_tab){    		
    		case "feeds":    			
    			$model =& $this->getModel("jaxmlimport");
    			$FeedID = JRequest::getVar("FeedID");
    			
					if ($model->storeField($FeedID)) {
						$msg = JText::_( 'Map field saved!' );
					} else {
						$msg = JText::_( 'Error saving map field' );
					}
    							
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    }
    
    /**
    * Save category record
    * 
    */
    function save(){
    	global $option;
    	switch ($this->_tab){
    		case "categories":
    			$model =& $this->getModel("jaxmlimport");		
				if ($model->storeCategory()) {
					$msg = JText::_( 'Category saved!' );
				} else {
					$msg = JText::_( 'Error saving category' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    		case "feeds":
    			
    			$model =& $this->getModel("jaxmlimport");		
				if ($model->storeItem()) {
					$msg = JText::_( 'Item saved!' );
				} else {
					$msg = JText::_( 'Error saving item' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    } 
    
    /**
    * Remove category record
    * 
    */
    function remove(){
    	global $option;
    	switch ($this->_tab){
    		case "imported":
    			$model =& $this->getModel("jajobs");		
				if ($model->remove()) {
					$msg = JText::_( 'Job deleted!' );
				} else {
					$msg = JText::_( 'Error deleting job' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    		case "feeds":
    			$model =& $this->getModel("jaxmlimport");	
				if ($model->deleteItems()) {
					$msg = JText::_( 'Item deleted!' );
				} else {
					$msg = JText::_( 'Error deleting item' );
				}
				
				// Check the table in so it can be edited.... we are done with it anyway
				$link = 'index.php?option=com_jajobboard&view=jaxmlimport&tab='.$this->_tab;
				$this->setRedirect($link, $msg);
    			break;
    	}
    }
	
    
	function saveorder()
    {
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);

        $model =& $this->getModel( 'jaxmlimport' );
        $msg = '';
        if ($model->setOrder($cid)) {
            $msg = JText::_( 'New ordering saved' );
        } else {
        	JError::raiseWarning(1, $model->getError());            
        }
        $this->setRedirect(  "index.php?option=$option&view=jaxmlimport&tab=categories", $msg );
    }
    
    
     
    function orderdown(){
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);
        
        $task = JRequest::getVar('task', 'orderdown');

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->setRedirect( "index.php?option=$option&view=jaxmlimport&tab=categories", JText::_('No Items Selected') );
            return false;
        }
		
        $model =& $this->getModel( 'jaxmlimport' );
        $movement = $task == 'orderdown' ? 1 : -1;

        if($model->orderItem($id, $movement)){
            $msg = $movement > 0 ? JText::_( 'Item Moved Down' ) : JText::_( 'Item Moved Up' );
        }
        
        $this->setRedirect( "index.php?option=$option&view=jaxmlimport&tab=categories", $msg );
    } 
    
    /**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function publish(){
		global  $option;
		if($this->getTask()=='unpublish'){
			$publish = 0;
			$msg = JText::_('Successfully unpublished '.JRequest::getVar("tab").'');
		}
		else{
			$msg = JText::_('Successfully published '.JRequest::getVar("tab").'');
			$publish = 1;
		}
		
		$model = $this->getModel('jaxmlimport');
				
		if (!$model->dopublish($publish)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		$this->setRedirect("index.php?option=$option&view=jaxmlimport&tab=".JRequest::getVar("tab")."", $msg);
	}
    
}